﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Diagnostics;
using System.IO;

namespace Manager2070.Library
{
    public class ProcessTools
    {
        public static bool StartProcess(string fullPath, string arguments)
        {
            try
            {
                Process p = new Process();
                ProcessStartInfo psi = new ProcessStartInfo();
                psi.WorkingDirectory = Path.GetDirectoryName(fullPath);
                psi.Arguments = arguments;
                psi.FileName = fullPath;
                p.StartInfo = psi;
                p.Start();

                /*
                if (string.IsNullOrEmpty(arguments))
                    Process.Start(fullPath);
                else
                    Process.Start(fullPath, arguments);
                */

                return true;
            }
            catch { return false; }
        }

        public static bool IsProcessRunning(string processName)
        {
            if (Process.GetProcessesByName(processName).Length > 0)
                return true;

            return false;
        }

        public ManagementEventWatcher CreateWatcherForProcessStart(string processName)
        {
            string queryString =
                "SELECT TargetInstance" +
                "  FROM __InstanceCreationEvent " +
                "WITHIN  3 " +
                " WHERE TargetInstance ISA 'Win32_Process' " +
                "   AND TargetInstance.Name = '" + processName + "'";

            // The dot in the scope means use the current machine
            string scope = @"\\.\root\CIMV2";

            // Create a watcher and listen for events
            ManagementEventWatcher watcher = new ManagementEventWatcher(scope, queryString);
            // watcher.EventArrived += ProcessStarted;
            // watcher.Start();
            return watcher;
        }

        public ManagementEventWatcher CreateWatcherForProcessEnd(string processName)
        {
            string queryString =
                "SELECT TargetInstance" +
                "  FROM __InstanceDeletionEvent " +
                "WITHIN  3 " +
                " WHERE TargetInstance ISA 'Win32_Process' " +
                "   AND TargetInstance.Name = '" + processName + "'";

            // The dot in the scope means use the current machine
            string scope = @"\\.\root\CIMV2";

            // Create a watcher and listen for events
            ManagementEventWatcher watcher = new ManagementEventWatcher(scope, queryString);
            //watcher.EventArrived += ProcessEnded;
            //watcher.Start();
            return watcher;
        }

        /*
        private void ProcessEnded(object sender, EventArrivedEventArgs e)
        {
            ManagementBaseObject targetInstance = (ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value;
            string processName = targetInstance.Properties["Name"].Value.ToString();
            Console.WriteLine(String.Format("{0} process ended", processName));
        }

        private void ProcessStarted(object sender, EventArrivedEventArgs e)
        {
            ManagementBaseObject targetInstance = (ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value;
            string processName = targetInstance.Properties["Name"].Value.ToString();
            Console.WriteLine(String.Format("{0} process started", processName));
        }
         */
    }
}
